/**
 * @author Haidar Osman
 */
var Relation = function(fromClass, toClass, relationObj) {
	this.fromClass = fromClass;	
	this.toClass = toClass;
	this.relaionObj=relationObj;
	var that = this;
	this.group = new Kinetic.Group({

	});

	var points = [{
		x : this.fromClass.headerRect.getAbsolutePosition().x,
		y : this.fromClass.headerRect.getAbsolutePosition().y
	}, {
		x : this.toClass.headerRect.getAbsolutePosition().x,
		y : this.toClass.headerRect.getAbsolutePosition().y
	}];
	this.relationLine = new Kinetic.Polygon({
		points : points,
		fill : "#00D2FF",
		stroke : "black",
		strokeWidth : 2
	});
	this.title = new Kinetic.Text({
		x : (this.relationLine.points[0].x+this.relationLine.points[1].x)/2,
		y : (this.relationLine.points[0].y+this.relationLine.points[1].y)/2,
		text : "",
		fontSize : fontSize || 10,
		fontFamily : fontFamily || "Calibri",
		textFill : styles.memberTextColor,
		//textStroke : "#444",
		padding : 3,
		align : "left",
		verticalAlign : "middle",
		name : header
	});
	
	this.group.add(this.relationLine);
	this.group.add(this.title);
	// this.fromClass.outgoingRelations.addRelation(this);
	// this.toClass.incomingRelations.addRelation(this);
	this.update = function() {
		var X1, X2, Y1, Y2;
		var x1 = this.fromClass.headerRect.getAbsolutePosition().x;
		var x2 = this.fromClass.headerRect.getAbsolutePosition().x + this.fromClass.headerRect.width;
		var x3 = this.toClass.headerRect.getAbsolutePosition().x;
		var x4 = this.toClass.headerRect.getAbsolutePosition().x + this.toClass.headerRect.width;

		var y1 = this.fromClass.headerRect.getAbsolutePosition().y;
		var y2 = this.fromClass.headerRect.getAbsolutePosition().y + this.fromClass.currentHeight;
		var y3 = this.toClass.headerRect.getAbsolutePosition().y;
		var y4 = this.toClass.headerRect.getAbsolutePosition().y + this.toClass.currentHeight;
				
		if(x2 < x3) {
			X1 = x2;
			X2 = x3;
		} else if(x3 <= x2) {
			if(x2 <= x4) {
				X1 = x2;
				X2 = x2;
			} else if(x1 <= x4) {
				if(x1<=x3)
				{
					X1 = x3;
					X2 = x3;
				}
				else{
					X1 = x1;
					X2 = x1;
				}
			} else {
				X1 = x1;
				X2 = x4;
			}
		}

		if(y2 < y3) {
			Y1 = y2;
			Y2 = y3;
		} else if(y3 <= y2) {
			if(y2 <= y4) {
				Y1 = y2;
				Y2 = y2;
			} else if(y1 <= y4) {
				Y1 = y4;
				Y2 = y4;
			} else {
				Y1 = y1;
				Y2 = y4;
			}
		}

		
		that.relationLine.points[0].x = X1;
		that.relationLine.points[1].x = X2;
		that.relationLine.points[0].y = Y1;
		that.relationLine.points[1].y = Y2;

	}
	
	
	
}